<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Dangle
 *
 * @author dragon.hu add @2016.3.24
 */
require_once 'BaseChannel.php';

//朋友玩
class Pengyouwan implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="9d9e4a4fb604af4d"; //秘钥
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //秘钥
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("pengyouwan", $_REQUEST);
		$request = file_get_contents("php://input");
		$orderLog->log("pengyouwan", $request);
		//var_dump($request);
		$dataarr = json_decode($request,true);
		//var_dump($dataarr);
		$tmpstr = $paymentKey.$dataarr['cp_orderid'].$dataarr['ch_orderid'].$dataarr['amount'];
		//var_dump($tmpstr);
		$mysign = md5($tmpstr);
		$sign = $dataarr['sign'];
		//var_dump($sign);
		//var_dump($mysign);
		$return = array();
		if($sign == $mysign)  
		{ //验证通过
			 
			//进行发货
			if(false == $orderLog->recordOrder(STATUS_SUCCESS, $dataarr['amount'], $dataarr['cp_orderid'], "pengyouwan", $platform,$dataarr['ch_orderid']))
			{
				$return['ack'] = -1;
				$return['msg'] = 'failure';
				$orderLog->log("pengyouwan",array("order"=>$dataarr['cp_orderid'],"money"=>$dataarr['amount'],"serverresult"=>"faild"));
			}else 
			{
				//支付成功
				$return['ack'] = 200;
				$return['msg'] = 'success';
				$orderLog->log("pengyouwan",array("order"=>$dataarr['cp_orderid'],"money"=>$dataarr['amount'],"serverresult"=>"success"));
			}
			 
		} else {
			//"$sig验证失败"
			$return['ack'] = -1;
			$return['msg'] = 'failure';
			$orderLog->log("pengyouwan","sign error sign:$sign mysign:$mysign");
		}
		$ret = json_encode($return);
		return $ret;
	}
	
}